package cn.e4j.modular.system.controller;

import cn.e4j.config.properties.E4jProperties;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwdhT;
import cn.e4j.modular.crm.entity.GwdheT;
import cn.e4j.modular.crm.entity.GwdhoT;
import cn.e4j.modular.crm.service.impl.GwdhTServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 公告资料管理
 *
 * @author shixc
 * @Date 2018/03/14
 */
@Controller
@RequestMapping("/sys/notice")
public class NoticeController extends BaseController {

    @Autowired
    private GwdhTServiceImpl gwdhTService;

    /**
     * 跳转公告资料管理页面
     */
    @RequiresPermissions("sys:notice:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/system/notice.html";
    }

    /**
     * 获取新增正式单号
     */
    @RequestMapping("/getAddData")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Dto addDataDto = Dtos.newDto();
        Date date = new Date();
        addDataDto.put("GWDHDOCNO", "GDZL" + DateUtils.formatDate(date, DateUtils.PATTERN_yyyyMMddHHmmss));
        addDataDto.put("GWDH004", user.getID());
        addDataDto.put("GWDH004_NAME", user.getNAME());
        addDataDto.put("GWDH005", DateUtils.formatDate(date, DateUtils.PATTERN_yyyy_MM_dd));
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 查询公告资料信息列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdhTService.getList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询人员开窗
     */
    @RequestMapping("/getUsersList")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData getUsersList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwdhTService.getUsersList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询组织开窗
     */
    @RequestMapping("/getOrgsList")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData getOrgsList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwdhTService.getOrgsList(page, inDto);
        return ResponseData.success(result);
    }

    /**
     * 查询客户开窗
     */
    @RequestMapping("/getGwakList")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData getGwakList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwdhTService.getGwakList(page, inDto);
        return ResponseData.success(result);
    }

    /**
     * 查询渠道开窗
     */
    @RequestMapping("/getGwaeList")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData getGwaeList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwdhTService.getGwaeList(page, inDto);
        return ResponseData.success(result);
    }

    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwdhT> gwdhTList = JSONObject.parseArray(jsonObject.getString("data"), GwdhT.class);
        List<GwdhoT> gwdhoTList = JSONObject.parseArray(jsonObject.getString("list1"), GwdhoT.class);
        List<GwdheT> gwdheTList = JSONObject.parseArray(jsonObject.getString("list2"), GwdheT.class);
        Dto dto = gwdhTService.addData(gwdhTList, gwdhoTList, gwdheTList);
        if (dto.getAppCode() == 400) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwdhT> gwdhTList = JSONObject.parseArray(jsonObject.getString("data"), GwdhT.class);
        List<GwdhoT> gwdhoTList = JSONObject.parseArray(jsonObject.getString("list1"), GwdhoT.class);
        List<GwdheT> gwdheTList = JSONObject.parseArray(jsonObject.getString("list2"), GwdheT.class);
        Dto dto = gwdhTService.editData(gwdhTList, gwdhoTList, gwdheTList);

        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData del(GwdhT gwdh) {
        gwdhTService.delete(gwdh);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwdhT> gwdhTList = inDto.getReqList(GwdhT.class);
        gwdhTService.delDataBatch(gwdhTList);
        return SUCCESS_TIP;
    }

    @RequestMapping("/getGwdhoData")
    @RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData getGwdhoData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwdhTService.getGwdhoData(inDto.getString("GWDHODOCNO"));
        return ResponseData.success(result);
    }

    @RequestMapping("/getDetailData")
    //@RequiresPermissions("sys:notice:init")
    @ResponseBody
    public ResponseData getDetailData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = gwdhTService.getDetailData(inDto.getString("GWDHDOCNO"));
        return ResponseData.success(result);
    }


}
